Repository navigation
[Engine] Metal: verify windows take the one-token kernels (opt-in STRATA_INK_M_INVARIANT=1, #6671) - #7639
Open
HundredBillion wants to merge 1 commit into
Open
[Engine] Metal: verify windows take the one-token kernels (opt-in STRATA_INK_M_INVARIANT=1, #6671)#7639HundredBillion wants to merge 1 commit into
HundredBillion wants to merge 1 commit into
Conversation
…ATA_INK_M_INVARIANT=1, Niko1221#6671) A verify window (M = 2..8 rows of one slot, as DFlash 2 and DSpark check drafts) did not return the logits a one-token step returns for the same positions, so drafting could change greedy tokens (Niko1221#6671 on Metal). - On every GPU the BF16 linear for M = 2..8 (ink_linear_bf16_mv, and ink_linear_bf16_mm8 for the 200K unembed) sums a row in another order than ink_linear_bf16_mv1: 4 weights per lane step against 8. - On Apple10 the attention trunk switches to MPP from M = 4..5 and S6 linears to sk6_mm8 from M = 2. With STRATA_INK_M_INVARIANT=1 (the switch Niko1221#6960 uses on CUDA), BF16 linears with M = 2..8 take the new ink_linear_bf16_mv_m1order (mv1's per-row summation order), the trunk MPP threshold is 9 (as the M5 Max EXACT mode's exact_verify_m1_match), and sk6_mm8 is skipped. It is not applied in the M5 Max FAST mode. Without the variable nothing changes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem and result
On Metal, a verify window (M = 2..8 rows of one slot, which is how DFlash 2 and DSpark check a draft) does not return the logits a one-token step returns for the same positions. Drafting keeps a token when the verify argmax matches it, so these differences can change greedy text (#6671 on Metal).
I measured it with
--spec-checkon an Apple M5 Pro: verify windows of 2, 3, 4 and 8 rows differed from the one-token steps in every row (max |dlogit| 0.003 at a 300-token prompt), and the hidden state already differed after visit 5.With this PR and
STRATA_INK_M_INVARIANT=1, every window row matches the one-token step bit for bit (max |dlogit| = 0, all four taps equal,RESULT: identical), at 300, 960, 1,100 and 3,000 prompt tokens. Past 1,024 tokens this needs #7098 as well (see Limits). Without the variable nothing changes.Related discussion
#6671 (drafting changes the greedy result; Metal, HIP and CUDA reports). #6960 is the CUDA side and uses the same
STRATA_INK_M_INVARIANT=1switch; this is the Metal side. #7098 fixes the SWA ring overwrite that also affects verify windows past 1,024 tokens.Approach
I traced which kernels a window takes that a one-token step does not (Inkling-2-Mini-SQv2-20, BF16 trunk):
STRATA_INK_M_INVARIANT=1ink_linear_bf16_mv(4 weights per lane step) and, for the 200K unembed,ink_linear_bf16_mm8; M = 1 runsink_linear_bf16_mv1(8 weights per lane step, two float4 dots added before the accumulate). The per-row sums round differently.ink_linear_bf16_mv_m1order: M = 2..8 rows, each row summed in exactly mv1's order (same lane-to-k assignment, same K & 4 tail on lane 0, samesimd_sum). Used for all BF16 linears with M = 2..8, the unembed included.exact_verify_m1_match.sk6_mm8from M = 2.F32 linears already sum M = 1 and M > 1 rows in the same order and are unchanged. MoE switches to MPP only from M = 10 on Apple10 (16 elsewhere), above a DFlash 2 window (kBlock = 8), so it is unchanged.
The switch is read at runner init, is not applied in the M5 Max FAST mode (lossy by design; its EXACT mode has
exact_verify_m1_match), and is printed asverify_m1=0|1in therunner readyline. The settings register has the new entry (tools/settings_register.py --checkpasses).Validation
Apple M5 Pro (20-core GPU, 64 GB), macOS 27.2.1,
Inkling-2-Mini-SQv2-20.strata, Release build (cmake -S engine/src/inkling2 ... && cmake --build ... --target strata-inkling). On the local branch the same code was switched by a variable namedSTRATA_INK_VERIFY_M1; it is renamed here to match #6960.Passed:
Build of this commit on
8db8753(main), Metal only (no CUDA, HIP or SYCL file is touched).python3 tools/settings_register.py --checkon this commit: exit 0.--spec-check 32 --prompt-ids @ids-300.txt --ctx 4096 --kv fp16on this commit on8db8753(DFlash 2 does not load here, so it checks the windows only):RESULT: DIFFERS, plain fingerprint7ecb44c57fd56dd6;STRATA_INK_M_INVARIANT=1: 0 rows differ at every M (max |dlogit| = 0), all four taps equal,RESULT: identical(exit 0), same plain fingerprint.--spec-check 32 --prompt-ids @ids-300.txt --ctx 4096 --kv fp16, on a local branch from4560d03(0.2.23) with this change, [Engine] Metal: honor --kv fp8 (FP8_E4M3) on any GPU, not only the M5 Max fast path #6983, [Engine] inkling2 Metal: enable DFlash 2 on Apple M4 Max >= 64 GiB (2.95x decode) #7366 and a local gate that lets DFlash 2 load on the M5 Pro:RESULT: DIFFERS;RESULT: identical.The same with prompts of 400, 600, 800 and 960 tokens:
RESULT: identical. At 1,100 and 3,000 tokens the windows differ by up to 3.6 / 3.95 in the logits until [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098 is added; with [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098 on top,RESULT: identicalat 960, 1,100 and 3,000.Default path: without the variable, the plain B = 1 fingerprint (
7ecb44c57fd56dd6) and the window and tap differences are the same as the build without this change.End to end through
strata serveon that local branch, DFlash 2 on, temperature 0, MBPP-sanitized test split (all 257 tasks), compared with the--draft offrun in the last row by full answer text (reasoning and answer), tests run in a sandbox:--draft off--draft off)--draft offThe
--draft offcolumn was generated on the build without [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098. [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098 also changes--draft offonce a context passes 1,024 tokens: all 7 answers that differed in the last row also differ between--draft offwith and without [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098. Regenerated with--draft offon the same build as the last row, 5 of the 7 match it exactly, so 255 of 257 are identical;--draft offrepeats exactly on the other two (tasks 160 and 286), which differ after about 2,150 generated tokens. I have not found why.--spec-check 2600(300-token prompt) on that build: speculative vs plain identical over 2,600 tokens (909 steps, 3,925 drafted).Decode with this change (no [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098), DFlash 2 on: 55.8 tok/s against 26.9 tok/s with
--draft offover the 257 answers, end to end, single request, idle machine.Failed:
Not run:
--spec-checkcovers the forward pass both drafters verify with.mv1/mvpair was not checked and is not changed.--parallelabove 1 and--kv fp8.Limits
mv/mm8/ MPP kernels, which round differently.--draft offthroughstrata servewith this change and [Engine] Metal: fix SWA KV ring overwrite during chunked prefill (#7024) #7098; not explained yet.🤖 Generated with Claude Code